{% extends 'layout.html' %}
{% load static %}
{% block content %}
    <div class="container">
        <div class="panel panel-default">
            <div class="panel-heading">折线图</div>
            <div class="panel-body">
                <div id="panelLine" style="width: 100%;height:400px;"></div>
            </div>
        </div>
        <div class="row">
            <div class="col-xs-8">
                <div class="panel panel-default">
                    <div class="panel-heading">柱状图</div>
                    <div class="panel-body">
                        <div id="main" style="width: 100%;height:400px;"></div>
                    </div>
                </div>
            </div>
            <div class="col-xs-4">
                <div class="panel panel-default">
                    <div class="panel-heading">饼图</div>
                    <div class="panel-body">
                        <div id="panel_pie" style="width: 100%;height:400px;"></div>
                    </div>
                </div>
            </div>
        </div>
    </div>
{% endblock %}
{% block js %}
    <script src="{% static 'js/echarts.js' %}"></script>
    <script type="text/javascript">
        $(function () {
            initBar();
            initPie();
            initLine();
        })

        function initLine() {
            // 基于准备好的dom，初始化echarts实例
            var myChart = echarts.init(document.getElementById('panelLine'));

            // 指定图表的配置项和数据
            var option = {
                xAxis: {
                    type: 'category',
                    data: []
                },
                yAxis: {
                    type: 'value'
                },
                series: [
                    {
                        data: [],
                        type: 'line',
                        smooth: true
                    }
                ]
            };
            $.ajax({
                url: '/chart/line/',
                type: 'get',
                dataType: 'JSON',
                success: function (res) {
                    if (res.status) {
                        option.xAxis.data = res.data.create_time;
                        option.series[0].data = res.data.account;
                        // 使用刚指定的配置项和数据显示图表。
                        myChart.setOption(option);
                    } else {
                        console.log('NO DATA!')
                    }
                }
            });
        }

        function initBar() {
            // 基于准备好的dom，初始化echarts实例
            var myChart = echarts.init(document.getElementById('main'));

            // 指定图表的配置项和数据
            var option = {
                title: {
                    text: '账户余额'
                },
                tooltip: {},
                legend: {
                    data: ['账户余额'],
                    bottom: 0
                },
                xAxis: {
                    data: []
                },
                yAxis: {},
                series: [
                    {
                        name: '账户余额',
                        type: 'bar',
                        data: []
                    }
                ]
            };
            $.ajax({
                url: '/chart/bar/',
                type: 'get',
                dataType: 'JSON',
                success: function (res) {
                    if (res.status) {
                        option.xAxis.data = res.data.create_time;
                        option.series[0].data = res.data.account;
                        // 使用刚指定的配置项和数据显示图表。
                        myChart.setOption(option);
                    } else {
                        console.log('NO DATA!')
                    }
                }
            });
        }

        function initPie() {
            // 基于准备好的dom，初始化echarts实例
            var myChart = echarts.init(document.getElementById('panel_pie'));

            // 指定图表的配置项和数据
            var option = {
                tooltip: {
                    trigger: 'item'
                },
                legend: {
                    top: '5%',
                    left: 'center',
                },
                series: [
                    {
                        name: '订单构成',
                        type: 'pie',
                        radius: ['40%', '70%'],
                        avoidLabelOverlap: false,
                        itemStyle: {
                            borderRadius: 10,
                            borderColor: '#fff',
                            borderWidth: 2
                        },
                        label: {
                            show: false,
                            position: 'center'
                        },
                        emphasis: {
                            label: {
                                show: true,
                                fontSize: 40,
                                fontWeight: 'bold'
                            }
                        },
                        labelLine: {
                            show: false
                        },
                        data: []
                    }
                ]
            };


            $.ajax({
                url: '/chart/pie/',
                type: 'get',
                dataType: 'JSON',
                success: function (res) {
                    if (res.status) {
                        option.series[0].data = res.data;
                        // 使用刚指定的配置项和数据显示图表。
                        myChart.setOption(option);
                    } else {
                        console.log('NO DATA!')
                    }
                }
            });
        }
    </script>
{% endblock %}